/**
 * 
 */
package cn.tonyqiu.wonuly.client.gui;

import java.util.List;

import cn.tonyqiu.wonuly.client.rpc.RPCGenerator;
import cn.tonyqiu.wonuly.client.vo.BillVo;

import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ChangeListener;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

/**
 * @author <a href="mailto:tony.qiu.smart@gmail.com">Tony Qiu</a>
 * 
 */
public class AddPanel extends Composite implements ClickListener{

	private VerticalPanel outer = new VerticalPanel();
	private HorizontalPanel accountPanel = new HorizontalPanel();
	private Label accountLabel = new Label(Wonuly.constants
			.addPanelBillAccount());
	private ListBox accountListBox = new ListBox();

	private HorizontalPanel categoryPanel = new HorizontalPanel();
	private Label categoryLabel = new Label(Wonuly.constants
			.addPanelBillCategory());
	private ListBox categoryListBox = new ListBox();

	private HorizontalPanel amountPanel = new HorizontalPanel();
	private Label amountLabel = new Label(Wonuly.constants.addPanelBillAmount());
	private TextBox amountTextBox = new TextBox();

	private HorizontalPanel datePanel = new HorizontalPanel();
	private Label dateLabel = new Label(Wonuly.constants.addPanelBillDate());
	private TextBox dateTextBox = new TextBox();
	private CalendarPopup calendar = new CalendarPopup();

	private HorizontalPanel commentPanel = new HorizontalPanel();
	private Label commentLabel = new Label(Wonuly.constants
			.addPanelBillComment());
	private TextArea commentTextArea = new TextArea();
	
	private HorizontalPanel optionPanel = new HorizontalPanel();
	private Button addButton = new Button(Wonuly.constants.addPanelBillAdd());
	private Button addAndNextButton = new Button(Wonuly.constants.addPanelBillAddAndNext());
	private Button cancelButton = new  Button(Wonuly.constants.buttonCancel());

	public AddPanel() {
		accountPanel.add(accountLabel);
		initAccountListBox();
		accountPanel.add(accountListBox);
		accountPanel.setSpacing(4);
		outer.add(accountPanel);
		
		categoryPanel.add(categoryLabel);
		initCategoryListBox();
		categoryPanel.add(categoryListBox);
		outer.add(categoryPanel);
		
		datePanel.add(dateLabel);
		
		calendar.addChangeListener(new ChangeListener() {
			public void onChange(Widget w) {
				dateTextBox.setText(calendar.getDate().toString());
				calendar.hide();
				calendar.setDate(calendar.getDate());
			}
			
		});
		
		dateTextBox.addClickListener(new ClickListener() {
			public void onClick(Widget w) {
				int left = w.getAbsoluteLeft()+w.getOffsetWidth();
				int top = w.getAbsoluteTop();
				calendar.setPopupPosition(left, top);
				calendar.show();
			}
		});
		datePanel.add(dateTextBox);
		outer.add(datePanel);
		
		amountPanel.add(amountLabel);
		amountPanel.add(amountTextBox);
		outer.add(amountPanel);
		
		commentPanel.add(commentLabel);
		commentPanel.add(commentTextArea);
		outer.add(commentPanel);
		
		addButton.addClickListener(this);
		addAndNextButton.addClickListener(this);
		cancelButton.addClickListener(this);
		optionPanel.add(addButton);
		optionPanel.add(addAndNextButton);
		optionPanel.add(cancelButton);
		outer.add(optionPanel);
		
		initWidget(outer);
	}

	private void initAccountListBox() {
		RPCGenerator.getLookupAsync().getAccountByUser(
				new AsyncCallback<List<String>>() {
					public void onFailure(Throwable arg0) {
						Window.alert("Exception:" + arg0);
					}

					public void onSuccess(List<String> accounts) {
						for (String s : accounts) {
							accountListBox.addItem(s);
						}
					}
				});
	}
	
	private void initCategoryListBox() {
		RPCGenerator.getLookupAsync().getCategoryByUser(new AsyncCallback<List<String>>() {

			public void onFailure(Throwable arg0) {
				Window.alert("Exception:" + arg0);
			}

			public void onSuccess(List<String> categories) {
				for(String s: categories) {
					categoryListBox.addItem(s);
				}
				
			}
			
		});
	}

	/* (non-Javadoc)
	 * @see com.google.gwt.user.client.ui.ClickListener#onClick(com.google.gwt.user.client.ui.Widget)
	 */
	public void onClick(Widget w) {
		if(w == addButton) {
			BillVo bill = new BillVo();
			bill.setAccount(accountListBox.getItemText(accountListBox.getSelectedIndex()));
			bill.setAmount(amountTextBox.getText());
			bill.setCategory(categoryListBox.getItemText(categoryListBox.getSelectedIndex()));
			bill.setComment(commentTextArea.getText());
			bill.setDate(dateTextBox.getText());
			RPCGenerator.getBillAsync().addBill(bill, new AsyncCallback<BillVo>() {

				public void onFailure(Throwable arg0) {
					Window.alert("Exception:"+arg0);
					
				}

				public void onSuccess(BillVo arg0) {
					Window.alert("Succeed:"+arg0.getAccount());
					
				}
				
			});
		}else if(w == addAndNextButton) {
			Window.alert("To be implemented.");
		}else if(w == cancelButton) {
			Window.alert("To be implemented.");
		}
		
	}

}
